<template>
    <van-address-list 
        v-if="action == 'select'"
        v-model="active"
        :list="list"
        default-tag-text="默认"
        @add="AddressAdd"
        @edit="AddressEdit"
        @select="AddressSelect"
    />
    <van-address-list
        v-else
        v-model="active"
        :list="list"
        default-tag-text="默认"
        @add="AddressAdd"
        @edit="AddressEdit"

    />
    <!-- @select="AddressCheck" -->
</template>

<script setup>
    const {proxy} = getCurrentInstance()

    let business = proxy.$business.value
    
    let action  = proxy.$route.query.action ? proxy.$route.query.action : ''
    action = ref(action)

    let active = ref(0)
    onMounted( () =>{
        Address()
    })

    //切换默认地址
    // const AddressCheck = async (item) =>
    // {
    //     //组装数据
    //     var params = {
    //         id: item.id,
    //         busid: business.id
    //     }

    //     var result = await proxy.$POST({url: '/address/check', params})

    //     if(result.code == "0")
    //     {
    //         proxy.$fail(result.msg)
    //         return false
    //     }

    //     list.map((value) => value.isDefault = value.id == item.id ? true : false)
    // }    

    //收获地址列表
    let list = reactive([
        // {
        //     id:1,
        //     name: '张三',
        //     tel: '13512644556',
        //     address: '广州海珠区 新港西路230号',
        //     isDefault: true,
        // },
        // {
        //     id:2,
        //     name: '张三',
        //     tel: '13512644556',
        //     address: '广州海珠区 新港西路230号',
        //     isDefault: false,
        // },
        // {
        //     id:3,
        //     name: '张三',
        //     tel: '13512644556',
        //     address: '广州海珠区 新港西路230号',
        //     isDefault: false,
        // }
    ])
    
    //收货地址首页
    const Address = async () => {
        var result = await proxy.$POST({url: '/address/index', params: {busid: business.id}})

        if(result.code == 0)
        {
            proxy.fail(result.msg);
            return false
        }

        result.data.map((item) => {
            // console.log(item)
            if(item.status == '1') active.value = item.id

            list.push({
                id: item.id,
                name: item.consignee,
                tel: item.mobile,
                address: `${item.region_text} ${item.address}`,
                isDefault: item.status == 1 ? true : false 
            })
        })
        // list.sort(list.isDefault = true)
        list.sort((a, b) => b.isDefault - a.isDefault)
       
    }
    //收获地址添加
    const AddressAdd = async () => proxy.$router.push('/address/add')

    //收获地址的编辑
    const AddressEdit = async (item) => {
        proxy.$router.push({
            path: '/address/edit',
            query: {addid: item.id}
        })
    }

    //确认订单跳转 选择收货地址
    const AddressSelect = async (item) =>
    {
        item.region_text = item.address

        proxy.$address.value = item

        proxy.$router.go(-1)
    }
</script>